
using Nop.Core.Caching;
using Nop.Core.Data;
using Nop.Core.Domain.Common;
using Nop.Services.Events;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nop.Services.Common
{
    /// <summary>
    /// FlowSteps service
    /// </summary>
    public partial class FlowStepsService : IFlowStepsService
    {
        #region Constants

        /// <summary>
        /// Key for caching
        /// </summary>
        private const string FLOWSTEPSS_ALL_KEY = "Nop.flowsteps.all";
        /// <summary>
        /// Key for caching
        /// </summary>
        /// <remarks>
        /// {0} : flowSteps ID
        /// </remarks>
        private const string FLOWSTEPS_BY_ID_KEY = "Nop.flowsteps.id-{0}";
        /// <summary>
        /// Key pattern to clear cache
        /// </summary>
        private const string FLOWSTEPSS_PATTERN_KEY = "Nop.flowsteps.";
        #endregion
        
        #region Fields
        private readonly IRepository<FlowSteps> _flowStepsRepository;
        private readonly IRepository<Flow> _flowRepository;
        private readonly IEventPublisher _eventPublisher;
        private readonly ICacheManager _cacheManager;
        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="flowStepsRepository">FlowSteps repository</param>
        /// <param name="eventPublisher">Event published</param>
        public FlowStepsService(ICacheManager cacheManager,
            IRepository<FlowSteps> flowStepsRepository,
            IRepository<Flow> flowRepository,
            IEventPublisher eventPublisher)
        {
            this._cacheManager = cacheManager;
            this._flowStepsRepository = flowStepsRepository;
            this._flowRepository = flowRepository;
            this._eventPublisher = eventPublisher;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets all FlowStepss
        /// </summary>
        /// <returns>FlowStepss</returns>
        public virtual IList<FlowSteps> GetAllFlowStepss()
        {
            string key = FLOWSTEPSS_ALL_KEY;
            return _cacheManager.Get(key, () =>
            {
                var query = from a in _flowStepsRepository.Table
                            orderby a.Id
                            select a;
                return query.ToList();
            });
        }

        public virtual IList<FlowSteps> GetFlowStepss(string flowSystemName)
        {
            var query = _flowStepsRepository.Table
                        .Join(_flowRepository.Table, x => x.FlowId, y => y.Id, (x, y) => new { Steps = x, Flow = y })
                        .Where(z => z.Flow.SystemName == flowSystemName)
                        .Select(z => z.Steps);
            return query.ToList();
        }

        /// <summary>
        /// Gets a FlowSteps 
        /// </summary>
        /// <param name="flowStepsId">FlowSteps identifier</param>
        /// <returns>FlowSteps</returns>
        public virtual FlowSteps GetFlowStepsById(int flowStepsId)
        {
            if (flowStepsId == 0)
                return null;

            string key = string.Format(FLOWSTEPS_BY_ID_KEY, flowStepsId);
            return _cacheManager.Get(key, () => _flowStepsRepository.GetById(flowStepsId));
        }

        /// <summary>
        /// Inserts a FlowSteps
        /// </summary>
        /// <param name="flowSteps">FlowSteps</param>
        public virtual void InsertFlowSteps(FlowSteps flowSteps)
        {
            if (flowSteps == null)
                throw new ArgumentNullException("flowSteps");

            _flowStepsRepository.Insert(flowSteps);

            _cacheManager.RemoveByPattern(FLOWSTEPSS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(flowSteps);
        }

        /// <summary>
        /// Updates the FlowSteps
        /// </summary>
        /// <param name="flowSteps">FlowSteps</param>
        public virtual void UpdateFlowSteps(FlowSteps flowSteps)
        {
            if (flowSteps == null)
                throw new ArgumentNullException("flowSteps");

            _flowStepsRepository.Update(flowSteps);

            _cacheManager.RemoveByPattern(FLOWSTEPSS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(flowSteps);
        }

        /// <summary>
        /// Deletes a FlowSteps
        /// </summary>
        /// <param name="flowSteps">FlowSteps</param>
        public virtual void DeleteFlowSteps(FlowSteps flowSteps)
        {
            if (flowSteps == null)
                throw new ArgumentNullException("flowSteps");

            _flowStepsRepository.Delete(flowSteps);

            _cacheManager.RemoveByPattern(FLOWSTEPSS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityDeleted(flowSteps);
        }

        #endregion
    }
}
